package com.assistant.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;

public class FileHandle {
    private String sourcePath;
    private String targetPath;
    private String filename;
    private FileOutputStream logFile;

    /**
     *
     * @param sourcePath  只需要选择反编译的目录就可以
     * @param targetPath  需要选择 UserSdk 目录下的Android 目录
     * @param filename    需要替换的文件，默认 libQsu.so
     * @param logFile     日志文件
     */
    public FileHandle(String sourcePath, String targetPath, String filename, FileOutputStream logFile) {
        this.sourcePath = sourcePath;
        this.targetPath = targetPath;
        this.filename = filename;
        this.logFile=logFile;
    }

    //替换 sdk
    public void replaceAll(){
        StringBuilder stringBuilder=new StringBuilder();
        File archDir=new File(sourcePath+CommonDefault.getPlatformBASeparator("lib"));
        File targetFile=new File(targetPath);
        writeLog("now start replace the Directories source and target:"+archDir.getAbsolutePath()+"  "+targetFile.getAbsolutePath());
        for(File sFile : Objects.requireNonNull(archDir.listFiles())){
            for(File tFile: Objects.requireNonNull(targetFile.listFiles())){
                    if(sFile.getName().equals(tFile.getName())){
                        writeLog("now start replace the source and target:"+sFile.getAbsolutePath()+"  "+tFile.getAbsolutePath());
                        replaceFile(new File(sFile,filename),new File(tFile,filename));
                    }
            }
        }
    }

    private void replaceFile(File source,File target){
        if(source.exists()){
            writeLog("there is a source file:"+source.getAbsolutePath()+" now is deleting");
            source.delete();
        }
        if(!target.renameTo(source)){
            writeLog("rename file "+target.getAbsolutePath()+" can\'t success!");
        }
    }

    private void writeLog(String content){
        try {
            logFile.write((content+"\n").getBytes());
        } catch (IOException e) {
            System.out.println("can\'t write to log file!!");
            e.printStackTrace();
        }
    }

    public static class Builder{
        private String sourcePath;
        private String targetPath;
        private String filename;
        private FileOutputStream logFile;
        //必须要有日志文件
        private Builder(FileOutputStream logFile) {
            this.logFile = logFile;
        }

        public static Builder getInstance(FileOutputStream logFile){
            return new Builder(logFile);
        }
        public Builder setSourcePath(String sourcePath) {
            this.sourcePath = sourcePath;
            return this;
        }

        public Builder setTargetPath(String targetPath) {
            this.targetPath = targetPath;
            return this;
        }

        public Builder setFilename(String filename) {
            this.filename = filename;
            return this;
        }

        public FileHandle Build(){
            if(sourcePath==null){
                sourcePath="";
            }
            if(targetPath==null){
                targetPath="";
            }
            if(filename==null){
                filename="libQsu.so";
            }
            return new FileHandle(sourcePath,targetPath,filename,logFile);
        }
    }
}
